<template>
  <div class="top-head">
    <div class="banxin flex">
      <div class="welcom">欢迎来到叩丁严选</div>
      <div class="right flex">
        <ul class="flex">
          <li>
            <img :src="userInfo.headImg" alt="" />
            <span>用户名：</span>
            <span>{{ userInfo.nickName }}</span>
          </li>
          <li>
            <span>我的鸡腿： </span>
            <span>{{ userInfo.coin }}</span>
          </li>
          <li>
            <span>获取鸡腿</span>
          </li>
          <li>
            <span>叩丁狼官网</span>
          </li>
        </ul>
        <div
          v-show="!isLogin"
          class="btn"
          @click="$store.commit('Yinchan', true)"
        >
          登录
        </div>
        <div v-show="isLogin" class="btn">
          <img src="../../assets/images/Shopping.png" alt="" />
          <span>购物车</span>
          <span class="total">{{ cartTotal }}</span>
        </div>
      </div>
    </div>
  </div>
</template>
 
<script>
import { getUserInfoApi, WXLoginApi } from "../http/api";
import headImg from "../../assets/images/userImg.png";
import { toas } from "../../utils/toas";
export default {
  data() {
    return {
      userInfo: {
        headImg,
        nikName: "---",
        coin: "---",
      },
      cartTotal: 0,
      isLogin: false,
    };
  },
  created() {
    // console.log(this.$route.query.code);
    const code = this.$route.query.code;
    // //证明这是扫码登陆后跳转的
    if (code) {
      WXLoginApi(code).then((res) => {
        // console.log(res);
        if (res.code === 0) {
          localStorage.setItem("x-auth-token", res["x-auth-token"]);
          toas("微信登入成功", "success");
          this.getUserInfo();
        }
      });
    }

    this.getUserInfo();
  },
  methods: {
    async getUserInfo() {
      const token = localStorage.getItem("x-auth-token");
      if (!token) return;
      this.isLogin = true;

      const res = await getUserInfoApi();
      console.log(res);
      if (res.code === 0) {
        this.userInfo = res.data.userInfo;
        this.cartTotal = res.data.cartTotal;
      }
    },
  },
};
</script>
 
<style lang = "less" scoped>
.top-head {
  color: #fff;
  font-size: 14px;
  height: 40px;
  line-height: 40px;
  background: #242b39;
  .banxin {
    justify-content: space-between;
  }
  .right {
    ul {
      li {
        cursor: pointer;
        margin-right: 20px;
        img {
          width: 26px;
          height: 26px;
          vertical-align: middle;
          margin-right: 15px;
          border-radius: 50%;
        }
      }
    }
    .btn {
      cursor: pointer;
      width: 124px;
      text-align: center;
      background: #0a328e;
    }
  }
}
</style>